package bitCalculation;

public class BinaryGap868 {
    public int binaryGap(int n) {
        int result = 0;
        int begin = -1;
        int index = 0;
        while (n != 0) {
            if ((n & 1) == 1) {
                if (begin != -1) {
                    result = Math.max(result, index - begin);
                }
                begin = index;
            }
            index++;
            n = n >> 1;
        }
        return result;
    }
}
